package com.example.servicemodule;

import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.util.Log;
import android.widget.RemoteViews;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;


public class remoteService extends Service {
    private static final String TAG = "remoteService";
    private Student student;

    public static Bitmap mBitmap = Bitmap.createBitmap(466, 466, Bitmap.Config.ARGB_8888);

    static RemoteCallbackList<OnInfoChangeAidlInterface> mListenerList = new RemoteCallbackList<>();

    private ServiceAidlInterface.Stub serviceBinder = new ServiceAidlInterface.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public String getInfo() throws RemoteException {
            return "today is 2020/1/1";
        }

        @Override
        public Student getStudentInfo() throws RemoteException {
            return student;
        }

        @Override
        public RemoteViews getViews() throws RemoteException {
            return new RemoteViews(getPackageName(), R.layout.itemview);
        }

        @Override
        public void registerListener(OnInfoChangeAidlInterface listener) throws RemoteException {
            mListenerList.register(listener);
            int n = remoteService.mListenerList.beginBroadcast();
            Log.i(TAG, "registerListener: mListenerList" + n);
            mListenerList.finishBroadcast();
        }

        @Override
        public void unregisterListener(OnInfoChangeAidlInterface listener) throws RemoteException {
            mListenerList.unregister(listener);
            int n = remoteService.mListenerList.beginBroadcast();
            Log.i(TAG, "unregisterListener: mListenerList" + n);
            mListenerList.finishBroadcast();
        }

        @Override
        public ViewInfo getViewInfo() throws RemoteException {
            ViewInfo viewInfo = new ViewInfo();
            viewInfo.setName("ViewInfo Name is 远程视图");
            RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.itemview);
            remoteViews.setImageViewResource(R.id.imageView, R.drawable.ic_launcher_foreground);
            remoteViews.setTextViewText(R.id.textView, "刘旭东传递自定义对象");
            viewInfo.setRemoteViews(remoteViews);
            return viewInfo;
        }

        @Override
        public Bitmap getBitmap() throws RemoteException {
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            Canvas canvas = new Canvas(mBitmap);
            canvas.drawCircle(200, 200, 200, paint);
            return mBitmap;
        }
    };


    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: " + Thread.currentThread().getName());
        super.onCreate();
        student = new Student();
        student.setName("liuxudong");
        student.setAge(24);
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: " + Thread.currentThread().getName());
        return serviceBinder;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }
}
